import time
from 分治算法alg import can_partition
from 分治算法gen import generate_test_cases

def run_tests():
    """运行测试用例"""
    test_cases = generate_test_cases()
    passed = 0
    failed = 0
    total_time = 0
    
    for i, nums in enumerate(test_cases):
        expected = manual_check(nums)
        
        start_time = time.time()
        result = can_partition(nums)
        end_time = time.time()
        
        elapsed = end_time - start_time
        total_time += elapsed
        
        if result == expected:
            passed += 1
            status = "PASS"
        else:
            failed += 1
            status = "FAIL"
        
        print(f"测试用例 {i+1}: {status}")
        print(f"输入: {nums}")
        print(f"预期: {expected}, 实际: {result}")
        print(f"耗时: {elapsed:.6f}s")
        print("-" * 50)
    
    print(f"测试结果: 通过 {passed}, 失败 {failed}, 总数 {len(test_cases)}")
    print(f"总耗时: {total_time:.6f}s")
    print(f"平均耗时: {total_time/len(test_cases):.6f}s")

def manual_check(nums):
    """手动检查是否可以分割（用于验证）"""
    total = sum(nums)
    if total % 2 != 0:
        return False
    
    target = total // 2
    
    # 对于小规模数据，使用暴力法验证
    if len(nums) <= 20:
        from itertools import combinations
        for r in range(1, len(nums) + 1):
            for subset in combinations(nums, r):
                if sum(subset) == target:
                    return True
        return False
    else:
        # 对于大规模数据，假设生成器正确
        # 这里仅用于测试框架，实际应用中应使用更可靠的验证方法
        return can_partition(nums)

if __name__ == "__main__":
    run_tests()    